﻿using ShopPowerTemplate.Common;
using ShopPowerTemplate.Entities;
using ShopPowerTemplate.WebServiceReference;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage.Streams;
using ShopPowerTemplate.ViewModels;

namespace ShopPowerTemplate.ViewModels
{
    public class ProductPresenterDetailViewModel : BindableBase
    {
        public ICommand AddProductCommand
        {
            get
            {
                return App.ShoppingCart.AddProductCommand;
            }
        }

        Product _product = null;
        public Product Product
        {
            get { return this._product; }
            set
            {
                if (this.SetProperty(ref this._product, value))
                    InitializeDetailsFromProduct();
            }
        }

        ObservableCollection<ImagePresenterViewModel> _imagePresenterList = new ObservableCollection<ImagePresenterViewModel>();
        public ObservableCollection<ImagePresenterViewModel> ImagePresenterList
        {
            get { return this._imagePresenterList; }
            set { this.SetProperty(ref this._imagePresenterList, value); }
        }

        ObservableCollection<ProductOptionsViewModel> productOptions = new ObservableCollection<ProductOptionsViewModel>();
        public ObservableCollection<ProductOptionsViewModel> ProductOptions
        {
            get { return this.productOptions; }
            set { this.SetProperty(ref this.productOptions, value); }
        }

        ObservableCollection<Review> reviews = new ObservableCollection<Review>();
        public ObservableCollection<Review> Reviews
        {
            get { return this.reviews; }
            set { this.SetProperty(ref this.reviews, value); }
        }

        #region Constructors

        public ProductPresenterDetailViewModel(Product product) 
        {
            //this.PropertyChanged += ProductPresenterDetailViewModel_PropertyChanged;
            Product = product; 
        }
        #endregion Constructors

       public async void InitializeDetailsFromProduct()
        {
            WebServiceClient service = new WebServiceClient();
            ObservableCollection<ProductOptionsViewModel> po = new ObservableCollection<ProductOptionsViewModel>(); 
            var images = await service.GetProductAllImageAsync(Product.Id);
            images = new ObservableCollection<Image>(images.OrderBy(x => x.SortOrder).ToList());
            foreach (var image in images) ImagePresenterList.Add(new ImagePresenterViewModel(image));

            var optionTypes = await service.GetOptionTypesAsync(Product.Id);
            foreach (var optionType in optionTypes)
            {
                ProductOptionsViewModel optionsModel = 
                    new ProductOptionsViewModel 
                    { 
                        OptionType = optionType, 
                        Options = await service.OptionTypeGetOptionsForAsync(optionType) 
                    };

                //if (ProductDetail != null)
                //{
                //    int selectedOptionId = ProductDetail.Option.First(op => op.OptionType.Id == optionType.Id).Id;
                //    optionsModel.SelectedOption = optionsModel.Options.Single(o => o.Id == selectedOptionId);
                //}

                //Tito Trabajamos sobre una lista auxiliar para que se provoque el evento de cambio de ProductOptions
                //ProductOptions.Add(optionsModel);
                po.Add(optionsModel);
                //Fin Tito
            }
           
            ProductOptions = po; //Tito 

            Reviews = await service.ProductGetReviewsForAsync(Product);
        }

        //public async void InitializeDetailsFromProductDetail()
        //{
        //    WebServiceClient service = new WebServiceClient();
        //    ProductDetail.Option = (await service.GetProductDetailOptionsAsync(ProductDetail)).ToList();
        //    Product = await service.FindProductAsync(ProductDetail.Id);
        //}

        public virtual async void FillShareInfo(DataRequest request)
        {
            //TODO fill information to share
            request.Data.Properties.Title = Product.IdentityDescription;
            request.Data.Properties.Description = Product.Description;

            // Share text
            request.Data.SetText(Product.Description);

            // Share image
            if (ImagePresenterList != null && ImagePresenterList.Count > 0)
            {
                var reference = RandomAccessStreamReference.CreateFromStream(await ZipHelper.GetImageAsStream(ImagePresenterList[0].Image.Data));
                request.Data.Properties.Thumbnail = reference;
                var deferral = request.GetDeferral();
                request.Data.SetBitmap(reference);
                deferral.Complete();
            }
        }

        //Tito Comprueba si hay stock del product detail, a partir del producto y la opción seleccionada
        public bool CanExecuteAddProduct()
        {
            WebServiceClient service = new WebServiceClient();
            if (Product.ProductDetails.Count() == 0)
                return false;
            Task<bool> t = service.StockAvailableAsync(Product, new ObservableCollection<Option>(ProductOptions.Select(p => p.SelectedOption)));
            t.Wait();
            return t.Result;
        }
    }
}
